{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "enabling-default",
   "metadata": {},
   "source": [
    "# 01. Basic Example\n",
    "A simple example on how to use the FDTD Library"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "academic-rough",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "tracked-hindu",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import fdtd\n",
    "import fdtd.backend as bd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "upset-drain",
   "metadata": {},
   "source": [
    "## Set Backend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "breathing-belief",
   "metadata": {},
   "outputs": [],
   "source": [
    "fdtd.set_backend(\"numpy\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "copyrighted-custody",
   "metadata": {},
   "source": [
    "## Constants"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "unlike-perfume",
   "metadata": {},
   "outputs": [],
   "source": [
    "WAVELENGTH = 1550e-9\n",
    "SPEED_LIGHT: float = 299_792_458.0  # [m/s] speed of light"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fourth-vocabulary",
   "metadata": {},
   "source": [
    "## Simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "desirable-thread",
   "metadata": {},
   "source": [
    "create FDTD Grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "vertical-south",
   "metadata": {},
   "outputs": [],
   "source": [
    "grid = fdtd.Grid(\n",
    "    (2.5e-5, 1.5e-5, 1),\n",
    "    grid_spacing=0.1 * WAVELENGTH,\n",
    "    permittivity=1.0,\n",
    "    permeability=1.0,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "balanced-motel",
   "metadata": {},
   "source": [
    "boundaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ordered-message",
   "metadata": {},
   "outputs": [],
   "source": [
    "# grid[0, :, :] = fdtd.PeriodicBoundary(name=\"xbounds\")\n",
    "grid[0:10, :, :] = fdtd.PML(name=\"pml_xlow\")\n",
    "grid[-10:, :, :] = fdtd.PML(name=\"pml_xhigh\")\n",
    "\n",
    "# grid[:, 0, :] = fdtd.PeriodicBoundary(name=\"ybounds\")\n",
    "grid[:, 0:10, :] = fdtd.PML(name=\"pml_ylow\")\n",
    "grid[:, -10:, :] = fdtd.PML(name=\"pml_yhigh\")\n",
    "\n",
    "grid[:, :, 0] = fdtd.PeriodicBoundary(name=\"zbounds\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "interesting-onion",
   "metadata": {},
   "source": [
    "sources"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "official-happening",
   "metadata": {},
   "outputs": [],
   "source": [
    "grid[50:55, 70:75, 0] = fdtd.LineSource(\n",
    "    period=WAVELENGTH / SPEED_LIGHT, name=\"linesource\"\n",
    ")\n",
    "grid[100, 60, 0] = fdtd.PointSource(\n",
    "    period=WAVELENGTH / SPEED_LIGHT, name=\"pointsource\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "facial-giant",
   "metadata": {},
   "source": [
    "detectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "included-cherry",
   "metadata": {},
   "outputs": [],
   "source": [
    "grid[12e-6, :, 0] = fdtd.LineDetector(name=\"detector\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "chronic-surprise",
   "metadata": {},
   "source": [
    "objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "corresponding-consequence",
   "metadata": {},
   "outputs": [],
   "source": [
    "grid[11:32, 30:84, 0:1] = fdtd.AnisotropicObject(permittivity=2.5, name=\"object\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "separate-variance",
   "metadata": {},
   "source": [
    "## Run simulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "genuine-audience",
   "metadata": {},
   "outputs": [],
   "source": [
    "grid.run(50, progress_bar=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "norman-bulgarian",
   "metadata": {},
   "source": [
    "## Visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "purple-generic",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(2, 3, squeeze=False)\n",
    "titles = [\"Ex: xy\", \"Ey: xy\", \"Ez: xy\", \"Hx: xy\", \"Hy: xy\", \"Hz: xy\"]\n",
    "\n",
    "fields = bd.stack(\n",
    "    [\n",
    "        grid.E[:, :, 0, 0],\n",
    "        grid.E[:, :, 0, 1],\n",
    "        grid.E[:, :, 0, 2],\n",
    "        grid.H[:, :, 0, 0],\n",
    "        grid.H[:, :, 0, 1],\n",
    "        grid.H[:, :, 0, 2],\n",
    "    ]\n",
    ")\n",
    "\n",
    "m = max(abs(fields.min().item()), abs(fields.max().item()))\n",
    "\n",
    "for ax, field, title in zip(axes.ravel(), fields, titles):\n",
    "    ax.set_axis_off()\n",
    "    ax.set_title(title)\n",
    "    ax.imshow(bd.numpy(field), vmin=-m, vmax=m, cmap=\"RdBu\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hearing-kelly",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "grid.visualize(z=0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
